<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="模型名称" prop="modelName">
                        <el-input v-model="dataForm.modelName" placeholder="模型名称"></el-input>
                </el-form-item>
                <el-form-item label="模型类型（物理模型、统计模型、机器学习模型）" prop="modelType">
                        <el-input v-model="dataForm.modelType" placeholder="模型类型（物理模型、统计模型、机器学习模型）"></el-input>
                </el-form-item>
                <el-form-item label="训练数据集路径" prop="trainingDataSet">
                        <el-input v-model="dataForm.trainingDataSet" placeholder="训练数据集路径"></el-input>
                </el-form-item>
                <el-form-item label="验证数据集路径" prop="validationDataSet">
                        <el-input v-model="dataForm.validationDataSet" placeholder="验证数据集路径"></el-input>
                </el-form-item>
                <el-form-item label="均方误差（性能指标）" prop="mse">
                        <el-input v-model="dataForm.mse" placeholder="均方误差（性能指标）"></el-input>
                </el-form-item>
                <el-form-item label="平均绝对误差（性能指标）" prop="mae">
                        <el-input v-model="dataForm.mae" placeholder="平均绝对误差（性能指标）"></el-input>
                </el-form-item>
                <el-form-item label="决定系数（性能指标）" prop="rSquared">
                        <el-input v-model="dataForm.rSquared" placeholder="决定系数（性能指标）"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="模型配置参数" prop="config">
                        <el-input v-model="dataForm.config" placeholder="模型配置参数"></el-input>
                </el-form-item>
                <el-form-item label="超参数配置（仅机器学习模型适用）" prop="hyperparameters">
                        <el-input v-model="dataForm.hyperparameters" placeholder="超参数配置（仅机器学习模型适用）"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    id: null,
                    modelName: null,
                    modelType: null,
                    trainingDataSet: null,
                    validationDataSet: null,
                    mse: null,
                    mae: null,
                    rSquared: null,
                    createdAt: null,
                    updatedAt: null,
                    config: null,
                    hyperparameters: null,
                },
                dataRule: {
                    id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
                    ],
                    modelName: [
                        { required: true, message: '模型名称不能为空', trigger: 'blur' }
                    ],
                    modelType: [
                        { required: true, message: '模型类型（物理模型、统计模型、机器学习模型）不能为空', trigger: 'blur' }
                    ],
                    trainingDataSet: [
                        { required: true, message: '训练数据集路径不能为空', trigger: 'blur' }
                    ],
                    validationDataSet: [
                        { required: true, message: '验证数据集路径不能为空', trigger: 'blur' }
                    ],
                    mse: [
                        { required: true, message: '均方误差（性能指标）不能为空', trigger: 'blur' }
                    ],
                    mae: [
                        { required: true, message: '平均绝对误差（性能指标）不能为空', trigger: 'blur' }
                    ],
                    rSquared: [
                        { required: true, message: '决定系数（性能指标）不能为空', trigger: 'blur' }
                    ],
                    createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
                    ],
                    updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
                    ],
                    config: [
                        { required: true, message: '模型配置参数不能为空', trigger: 'blur' }
                    ],
                    hyperparameters: [
                        { required: true, message: '超参数配置（仅机器学习模型适用）不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppPredictiveModel/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.apppredictivemodel
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppPredictiveModel/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>